package com.sx.jys.ui.vm

import android.content.Context
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.XPopup
import com.sos.widget.app.dialog.HintDialog
import com.sx.jys.app.net.HttpRequestCoroutine
import com.sx.jys.app.net.bean.CheckBean
import com.sx.jys.app.until.CacheUntil
import com.sx.jys.app.until.MutableLiveListData
import com.sx.jys.app.until.request2
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.livedata.StringLiveData

class CreateOrderVm : BaseViewModel() {

    var coinListLiveData = MutableLiveListData<CheckBean>()

    var multipleListLiveData = MutableLiveListData<CheckBean>()

    val checkCoinList = mutableListOf<CheckBean>()

    val multipleList = mutableListOf<CheckBean>()

    val incomeMaxValue = StringLiveData()

    val outcomeMaxValue = StringLiveData()

    val entrustedPriceValue = StringLiveData()

    fun initData() {
        MutableLiveListData.Builder<CheckBean>(multipleListLiveData)
            .add(CheckBean("1", "1倍"))
            .add(CheckBean("2", "2倍"))
            .add(CheckBean("3", "3倍"))
            .add(CheckBean("4", "4倍"))
            .add(CheckBean("5", "5倍"))
            .apply {
                if (CacheUntil.showMoreFun()) {
                    add(CheckBean("-1", "自定义"))
                }
            }
            .buildPost()
    }

    fun coinList() {
        request2({ HttpRequestCoroutine.coinList() }, {
            coinListLiveData.value = it.map { CheckBean(it.id.toString(), it.code) }
        }, isShowDialog = true)
    }


    fun setCoinConfig(context: Context, many: Boolean) {
        val status = if (many) "开多" else "开空"
        val checkCoin = checkCoinList.firstOrNull()
        val checkMultiple = multipleList.firstOrNull()
        if (checkCoin == null) {
            ToastUtils.showShort("请选择币种")
        } else if (checkMultiple == null) {
            ToastUtils.showShort("请选择下单倍数")
        } else {
            XPopup.Builder(context)
                .asConfirm(
                    "请确认你的设置",
                    "币种：${checkCoin.name}\n做单：${status}\n倍数：${checkMultiple.id}倍"
                ) {
                    request2({
                        HttpRequestCoroutine.recordingOrder(
                            id = checkCoin.id.orEmpty(),
                            code = checkCoin.name.orEmpty(),
                            orderType = many,
                            multiple = checkMultiple.id.orEmpty(),
                            stopPrice = incomeMaxValue.value,
                            losePrice = outcomeMaxValue.value,
                            entrustedPrice = entrustedPriceValue.value
                        )
                    }, {
                        incomeMaxValue.value = ""
                        outcomeMaxValue.value = ""
                        HintDialog.Builder(context)
                            .setIcon(HintDialog.ICON_FINISH)
                            .setMessage("设置成功")
                            .show()
                    }, isShowDialog = true)
                }.show()
        }
    }
}